#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import "FWCommon.h"
#import "FWBadgeView.h"


@implementation FWBadgeView


@synthesize label, text, smallestHeight;


static CGFloat const kBottomPadding = 3;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        self.autoresizesSubviews = YES;
        self.contentMode = UIViewContentModeRedraw;
        
        label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.textAlignment = UITextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor clearColor];
        label.frame = frame;
        
        [self addSubview:label];
        
        self.text = @"";
        
        [self setNeedsLayout];
    }
    
    return self;
}

- (void)dealloc
{
    [label release];
    [text release];
    
    [super dealloc];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Size management

- (void)setFrame:(CGRect)rect
{
    CGRect current = super.frame;
    super.frame = rect;
    
    if ((rect.size.width != current.size.width) || (rect.size.height != current.size.height))
        [self setNeedsDisplay];
}

- (UIFont*)fontThatFits:(CGRect)rect
{
    return [UIFont boldSystemFontOfSize:rect.size.height - kBottomPadding];
}

- (void)layoutSubviews
{
    CGRect rect = self.frame;
    self.label.frame = CGRectMake(0, 0, rect.size.width, rect.size.height);
}

- (CGSize)sizeThatFits:(CGSize)fits
{
    // Choose the height we'll use
    CGFloat height = fits.height;
    if (height > 24)
        height = 24;
    else if (height > 16)
        height = 16;
    
    if (height < smallestHeight)
        height = smallestHeight;

    // Get a font that will fit into our chosen height
    UIFont* font = [self fontThatFits:CGRectMake(0, 0, 320, height)];
    self.label.font = font;
    
    // Now get the size of the text at that font
    CGSize textSize = [self.text sizeWithFont:font];
    CGFloat width = textSize.width;

    //DebugLog(@"LI:FWTodo: Sizing badge - font space = {%f,%f}", textSize.width, textSize.height);
    
    // Make round if only a single character
    if ([text length] <= 1) {
        width = height;
        //DebugLog(@"LI:FWTodo: Single char so set badge width to height = %f", width);
    }
    else {
        // Allow some room at the ends for the end-caps
        width += 8;
        
        if (width < height) {
            width = height;
            //DebugLog(@"LI:FWTodo: Width less than height so set badge width to height = %f", width);
        }
    }

    DebugLog(@"LI:FWTodo: Sized badge = {%f,%f}", width, height);
    return CGSizeMake(width, height);
}


// ******************************************************************************************
#pragma mark -
#pragma mark Attribute access

- (void)setText:(NSString *)newText
{
    if (![text isEqualToString:newText]) 
        [self setNeedsLayout];
    
    [text release];
    text = newText;
    [text retain];

    label.text = text;

    //DebugLog(@"LI:FWTodo: Badge text set = %@", text);
}


// ******************************************************************************************
#pragma mark -
#pragma mark Drawing

- (void)drawRect:(CGRect)rect
{
    NSBundle* bundle = [NSBundle bundleForClass:[self class]];
    NSString* badge = self.frame.size.height >= 24 ? @"badge24" : @"badge16"; 

    //DebugLog(@"LI:FWTodo: Drawing badge = {{%f, %f} {%f %f}}", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    //DebugLog(@"LI:FWTodo: Loaded badge background %@", badge);

    NSString* path = [bundle pathForResource:badge ofType:@"png"];

    if (UIImage* image = [UIImage imageWithContentsOfFile:path]) {
        CGFloat leftCap = 0;
        CGFloat topCap = 0;
        
        if (self.frame.size.width > image.size.width)
            leftCap = image.size.width / 2 - 2;
        
        UIImage* newImage = [image stretchableImageWithLeftCapWidth:leftCap topCapHeight:topCap];
        [newImage drawInRect:rect];
    }
}


@end
